//
//  HDPhotoBrowserView.m
//  WeShare2.0
//
//  Created by Mike on 15/11/9.
//  Copyright © 2015年 WeShare. All rights reserved.
//

#import "HDPhotoBrowserView.h"
@interface HDPhotoBrowserView ()

{
    UITableView *_imageTableView;
//    UILabel *_indicateLabel;        //图片指示标签 e.g. 1/6
    CGRect _frame;
    UIView *_titleView;
    UILabel *_titleLabel;
}
@end

@implementation HDPhotoBrowserView

- (instancetype)initWithFrame:(CGRect)frame {
    _frame = frame;
    self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, SCREENFRAME.size.height)];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView {
    _imageTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.height, self.width) style:UITableViewStylePlain];
    _imageTableView.delegate = self;
    _imageTableView.dataSource = self;
    _imageTableView.center = CGPointMake(_frame.size.width/2, _frame.size.height/2);
    _imageTableView.transform = CGAffineTransformMakeRotation(-M_PI_2);
    _imageTableView.showsHorizontalScrollIndicator = NO;
    _imageTableView.showsVerticalScrollIndicator = NO;
    _imageTableView.pagingEnabled = YES;
    _imageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _imageTableView.backgroundColor = [UIColor blackColor];
    
    _indicateLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.width-55, _frame.size.height-30, 35, 20)];
    _indicateLabel.textAlignment = NSTextAlignmentCenter;
    _indicateLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:13];
    _indicateLabel.textColor = [UIColor hexStringToColor:@"#FFFFFF"];
    _indicateLabel.layer.shadowOffset=CGSizeMake(0.5, 0.5);
    _indicateLabel.layer.shadowColor=[UIColor blackColor].CGColor;
    _indicateLabel.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    _indicateLabel.layer.cornerRadius = 7;
    _indicateLabel.clipsToBounds = YES;
    
    [self addSubview:_imageTableView];
    [self addSubview:_indicateLabel];
    self.clipsToBounds = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self addGestureRecognizer:tapGesture];
}

- (void)setTitle:(NSString *)title
{
    _title = title;
    if (!_titleView) {
        _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, _frame.size.height-40, APPFRAME.size.width, 40)];
        CAGradientLayer * gradientBottomLayer = [CAGradientLayer layer];
        
        gradientBottomLayer.frame = _titleView.bounds;
        
        gradientBottomLayer.colors = [NSArray arrayWithObjects:
                                      (id)[UIColor colorWithWhite:0.0f alpha:0].CGColor,
                                      (id)[UIColor colorWithWhite:0.0f alpha:0.4].CGColor,
                                      (id)[UIColor colorWithWhite:0.0f alpha:0.2].CGColor,nil];
        [_titleView.layer insertSublayer:gradientBottomLayer atIndex:0];
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, _titleView.width-30, _titleView.height)];
        _titleLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
        _titleLabel.textColor = [UIColor whiteColor];
        [_titleView addSubview:_titleLabel];
        [self insertSubview:_titleView belowSubview:_indicateLabel];
        _titleLabel.text = title;
    }
}

- (void)setDataArr:(NSArray *)dataArr {
    _dataArr = dataArr;
    //解析数据
    _indicateLabel.text = [NSString stringWithFormat:@"1/%ld",_dataArr.count];
    [_imageTableView reloadData];
    
}

-(void)removeObserver {
    [_anchorScrollView removeObserver:self forKeyPath:@"contentOffset"];
}

- (void)setAnchorScrollView:(UIScrollView *)anchorScrollView {
    [_anchorScrollView removeObserver:self forKeyPath:@"contentOffset"];
    _anchorScrollView = anchorScrollView;
    [_anchorScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    [self resignFirstResponder];
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.anchorScrollView) {
        CGFloat offsetY = _anchorScrollView.contentOffset.y;
        if (offsetY <= 0) {
            if (!_anchorScrollView.decelerating) {
                UIEdgeInsets edgeInsets = UIEdgeInsetsMake(-offsetY, 0, 0, 0);
                _anchorScrollView.contentInset = edgeInsets;
            }
            self.top = offsetY;
            _imageTableView.center = CGPointMake(_frame.size.width/2, (-offsetY+_frame.size.height)/2);
            if (-offsetY>self.height-_frame.size.height-100) {
                _titleView.hidden = YES;
                _indicateLabel.top = self.height-100;
                _indicateLabel.left = (self.width-_indicateLabel.width)/2.0;
            } else {
                _titleView.top = -offsetY+_frame.size.height-40;
                _titleView.hidden = NO;
                _indicateLabel.top = -offsetY+_frame.size.height-30;
                _indicateLabel.left = self.width-55;
            }
        }
    }
}

- (void)tapAction
{
    if ([self.delegate respondsToSelector:@selector(photoBrowserViewTap:)]) {
        [self.delegate photoBrowserViewTap:self];
    }
}

#pragma mark - tableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.width;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cellId";
    HDPhotoBrowerCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[HDPhotoBrowerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.contentView.transform = CGAffineTransformMakeRotation(M_PI_2);
    }
    cell.rect = self.bounds;
    cell.imageName = ((propertyPicture *)_dataArr[indexPath.row]).path;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"selectCellIndexPath->(%ld %ld)",indexPath.section,indexPath.row);
}

#pragma mark - scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //计算页码
    _indicateLabel.text = [NSString stringWithFormat:@"%ld/%ld",(NSUInteger)(scrollView.contentOffset.y/self.width)+1,self.dataArr.count];
//    _tagLabel.text = [NSString stringWithFormat:@"%zi/%zi",index+1,self.dataArray.count];
}

@end

@implementation HDPhotoBrowerCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.photoImageView = [[DOImageView alloc] initWithFrame:CGRectZero];
    self.photoImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:self.photoImageView];
    self.clipsToBounds = NO;
}

- (void)setRect:(CGRect)rect {
    _rect = rect;
    self.photoImageView.frame = rect;
}
- (void)setImageName:(NSString *)imageName {
    _imageName = [imageName copy];
    self.photoImageView.imgURLStr = imageName;
}

@end
